<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" destroy-on-close
        :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="表名" prop="tableName">
                <el-input v-model="dataForm.tableName" placeholder="表名"></el-input>
            </el-form-item>
            <el-form-item label="表描述" prop="tableDesc">
                <el-input v-model="dataForm.tableDesc" placeholder="表描述"></el-input>
            </el-form-item>
            <el-form-item label="表版本" prop="version">
                <el-input v-model="dataForm.version" placeholder="表版本"></el-input>
            </el-form-item>
            <el-form-item label="乐观锁" prop="revision">
                <el-input v-model="dataForm.revision" placeholder="乐观锁"></el-input>
            </el-form-item>
            <el-form-item label="创建人" prop="createdBy">
                <el-input v-model="dataForm.createdBy" placeholder="创建人"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" prop="createdTime">
                <el-input v-model="dataForm.createdTime" placeholder="创建时间"></el-input>
            </el-form-item>
            <el-form-item label="更新人" prop="updatedBy">
                <el-input v-model="dataForm.updatedBy" placeholder="更新人"></el-input>
            </el-form-item>
            <el-form-item label="更新时间" prop="updatedTime">
                <el-input v-model="dataForm.updatedTime" placeholder="更新时间"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as tableinfoService from '@/api/bdms/tableinfo'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkTable: 0,
                    tableName: '',
                    tableDesc: '',
                    version: '',
                    revision: '',
                    createdBy: '',
                    createdTime: '',
                    updatedBy: '',
                    updatedTime: ''
                },
                dataRule: {
                    tableName: [{
                        required: true,
                        message: '表名不能为空',
                        trigger: 'blur'
                    }],
                    tableDesc: [{
                        required: true,
                        message: '表描述不能为空',
                        trigger: 'blur'
                    }],
                    version: [{
                        required: true,
                        message: '表版本不能为空',
                        trigger: 'blur'
                    }],
                    revision: [{
                        required: true,
                        message: '乐观锁不能为空',
                        trigger: 'blur'
                    }],
                    createdBy: [{
                        required: true,
                        message: '创建人不能为空',
                        trigger: 'blur'
                    }],
                    createdTime: [{
                        required: true,
                        message: '创建时间不能为空',
                        trigger: 'blur'
                    }],
                    updatedBy: [{
                        required: true,
                        message: '更新人不能为空',
                        trigger: 'blur'
                    }],
                    updatedTime: [{
                        required: true,
                        message: '更新时间不能为空',
                        trigger: 'blur'
                    }]
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkTable = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkTable) {
                        tableinfoService.gettableinfoinfo(this.dataForm.pkTable).then(data => {
                            this.dataForm.tableName = data.tableName
                            this.dataForm.tableDesc = data.tableDesc
                            this.dataForm.version = data.version
                            this.dataForm.revision = data.revision
                            this.dataForm.createdBy = data.createdBy
                            this.dataForm.createdTime = data.createdTime
                            this.dataForm.updatedBy = data.updatedBy
                            this.dataForm.updatedTime = data.updatedTime
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkTable': this.dataForm.pkTable || undefined,
                            'tableName': this.dataForm.tableName,
                            'tableDesc': this.dataForm.tableDesc,
                            'version': this.dataForm.version,
                            'revision': this.dataForm.revision,
                            'createdBy': this.dataForm.createdBy,
                            'createdTime': this.dataForm.createdTime,
                            'updatedBy': this.dataForm.updatedBy,
                            'updatedTime': this.dataForm.updatedTime
                        }
                        var tick = !this.dataForm.pkTable ? tableinfoService.addtableinfo(params) :
                            tableinfoService.updatetableinfo(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            }
        }
    }
</script>